package com.example.dictdemo.service.impl;

import com.example.dictdemo.model.vo.DictModel;
import com.example.dictdemo.service.CommonAPI;
import com.example.dictdemo.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 通用api实现
 *
 * @author dlp
 * @date 2024/08/20
 */
@Service
public class CommonAPIImpl implements CommonAPI {

    @Autowired
    private ISysDictService sysDictService;
    /**
     * 7普通字典的翻译
     *
     * @param code
     * @param key
     * @return
     */
    @Override
    public String translateDict(String code, String key) {
        return sysDictService.queryDictTextByKey(code, key);
    }

    /**
     * 6字典表的 翻译
     *
     * @param table
     * @param text
     * @param code
     * @param key
     * @return
     */
    @Override
    public String translateDictFromTable(String table, String text, String code, String key,String dataSource) {
        return sysDictService.queryTableDictTextByKey(table, text, code, key,dataSource);
    }

    /**
     * 14 普通字典的翻译，根据多个dictCode和多条数据，多个以逗号分割
     *
     * @param dictCodes 例如：user_status,sex
     * @param keys      例如：1,2,0
     * @return
     */
    @Override
    public Map<String, List<DictModel>> translateManyDict(String dictCodes, String keys) {
        List<String> dictCodeList = Arrays.asList(dictCodes.split(","));
        List<String> values = Arrays.asList(keys.split(","));
        //update-begin---author:chenrui ---date:20231221  for：[issues/#5643]解决分布式下表字典跨库无法查询问题------------
        return sysDictService.queryManyDictByKeys(dictCodeList, values);
        //update-end---author:chenrui ---date:20231221  for：[issues/#5643]解决分布式下表字典跨库无法查询问题------------
    }

    /**
     * 15 字典表的 翻译，可批量
     *
     * @param table
     * @param text
     * @param code
     * @param keys       多个用逗号分割
     * @param dataSource 数据源
     * @return
     */
    @Override
    public List<DictModel> translateDictFromTableByKeys(String table, String text, String code, String keys, String dataSource) {
        return sysDictService.queryTableDictTextByKeys(table, text, code, Arrays.asList(keys.split(",")), dataSource);
    }
}
